using COC.Debug;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using COC.CEvent;
using COC.Service;
using COC.Exception;
namespace COC.Window
{
    /// <summary>
    /// mainly active specific services which import from registed of valid services in CContext 
    /// </summary>
		public class CServiceActivator : MonoBehaviour
		{

				//private Dictionary<CEventType,string> aServices =new Dictionary<CEventType,string>();
				private static CEventDriven driver;
				private List<string> aServices ;
                public bool isSucc;

				void RetrieveAvailableServices ()
				{
						FieldInfo[] fieldsInfo = typeof(CContext).GetFields (BindingFlags.Public | BindingFlags.Static);
						foreach (FieldInfo fI in fieldsInfo) {
								string className = (string)fI.GetValue (null);
				         //Debug.Log("className = "+typeof(TestService).Name);
				          //Debug.Log("className="+fI.GetValue(null));
								//Types parent=System.Type.GetType(className).BaseType;
								aServices.Add (className);
						}
				}

				void BindServices ()
				{
						 if(aServices.Count>0){
				           
						 foreach(string sName in aServices){
								//string sName = enumerator.Current;
					       // string s=typeof(TestService).FullName;
					         //  string s=sName;
					           //Debug.Log(System.Type.GetType(s)+";"+"sName="+s.Substring(s.LastIndexOf(".")+1));

				               // Debug.Log("sName="+System.Type.GetType (enumerator.Current).Name);

                             ////TODO


								try {
									if (System.Type.GetType (sName).BaseType.Name.Equals(typeof(IService).Name)) {
											//IService service = (IService)System.Reflection.Assembly.GetExecutingAssembly ().CreateInstance (sName, true);
							                
							               // Debug.Log("start a server:"+service.serviceName);
							                driver.RegistService (sName, CEventType.UI_EVENT); ///only test
									}
								} catch (CException ex) {
					                throw new CException(ex.Message);
					                 //Debug.LogError(ex.Message,this);
								}
				}}
				}

                /*void Start ()
                {
                        StartCoroutine ("Init");
                }
       */
                #region  invoke method by task
                 public IEnumerator Init ()
				{
                    DebugTool.Log("ready to start the service activator");
			            driver=this.gameObject.AddComponent<CEventDriven>();
						driver.Init (this);
			            CreateServicesAndStart ();
						yield return null;
                        notifyStartServicesSucc();
                        yield return null;
				}
                #endregion

                void CreateServicesAndStart ()
				{
                    DebugTool.Log("retrieve services and start it separately");
			            aServices= new List<string> ();
						RetrieveAvailableServices ();
						BindServices ();  
				}
                void notifyStartServicesSucc()
                {
                    isSucc = true;
                }

               

		}

}